package ca.liu.scaffoldingmvctest.service;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for StUser entities
 * 
 */

@Service("StUserService")
@Transactional
public class StUserServiceImpl implements StUserService {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Instantiates a new StUserServiceImpl.
	 *
	 */
	public StUserServiceImpl() {
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@Transactional
	public StUser saveStUserStLogs(Integer id, StLog related_stlogs) {
		StUser stuser = stUserDAO.findStUserByPrimaryKey(id, -1, -1);
		StLog existingstLogs = stLogDAO.findStLogByPrimaryKey(related_stlogs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingstLogs != null) {
			existingstLogs.setId(related_stlogs.getId());
			existingstLogs.setValue(related_stlogs.getValue());
			related_stlogs = existingstLogs;
		} else {
			related_stlogs = stLogDAO.store(related_stlogs);
			stLogDAO.flush();
		}

		related_stlogs.setStUser(stuser);
		stuser.getStLogs().add(related_stlogs);
		related_stlogs = stLogDAO.store(related_stlogs);
		stLogDAO.flush();

		stuser = stUserDAO.store(stuser);
		stUserDAO.flush();

		return stuser;
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@Transactional
	public void saveStUser(StUser stuser) {
		StUser existingStUser = stUserDAO.findStUserByPrimaryKey(stuser.getId());

		if (existingStUser != null) {
			if (existingStUser != stuser) {
				existingStUser.setId(stuser.getId());
				existingStUser.setPassword(stuser.getPassword());
				existingStUser.setUsername(stuser.getUsername());
			}
			stuser = stUserDAO.store(existingStUser);
		} else {
			stuser = stUserDAO.store(stuser);
		}
		stUserDAO.flush();
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@Transactional
	public void deleteStUser(StUser stuser) {
		stUserDAO.remove(stuser);
		stUserDAO.flush();
	}

	/**
	 */
	@Transactional
	public StUser findStUserByPrimaryKey(Integer id) {
		return stUserDAO.findStUserByPrimaryKey(id);
	}

	/**
	 * Return a count of all StUser entity
	 * 
	 */
	@Transactional
	public Integer countStUsers() {
		return ((Long) stUserDAO.createQuerySingleResult("select count(o) from StUser o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@Transactional
	public StUser deleteStUserStLogs(Integer stuser_id, Integer related_stlogs_id) {
		StLog related_stlogs = stLogDAO.findStLogByPrimaryKey(related_stlogs_id, -1, -1);

		StUser stuser = stUserDAO.findStUserByPrimaryKey(stuser_id, -1, -1);

		related_stlogs.setStUser(null);
		stuser.getStLogs().remove(related_stlogs);

		stLogDAO.remove(related_stlogs);
		stLogDAO.flush();

		return stuser;
	}

	/**
	 * Return all StUser entity
	 * 
	 */
	@Transactional
	public List<StUser> findAllStUsers(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<StUser>(stUserDAO.findAllStUsers(startResult, maxRows));
	}

	/**
	 * Load an existing StUser entity
	 * 
	 */
	@Transactional
	public Set<StUser> loadStUsers() {
		return stUserDAO.findAllStUsers();
	}
}
